package cn.wolfcode.springcloud.controller;

import cn.wolfcode.springcloud.domain.Product;
import cn.wolfcode.springcloud.service.IProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController
//@RequestMapping("products")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/get/{id}")       //参数路径方式, 把参数也作为路径的一部分
    // "/{id}" {}:占位符 /{}直接将参数拼接在路径里,并且{}里的变量名最好与形式参数名相同
    //  @PathVariable 把参数设置到方法上的形式参数
    public Object get(@PathVariable Long id){
        Product product = productService.get(id);
        //能判断出使用的是集群中的哪个端口
        Product pp = new Product();
        //防止端口重复拼接
        BeanUtils.copyProperties(product,pp);
        pp.setName(product.getName() + "-" + port);
        return pp;
    }
}
